package com.papa91.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class HttpUtils {
	//public static final String EXIT_AD_URL = "http://testanv8.ctapi.papa91.com/advertisement/simulator_sign_out_ad";
	public static final String EXIT_AD_URL = "http://anv8.ctapi.papa91.com/advertisement/simulator_sign_out_ad";
	protected static final int SHOW_RESPONSE = 0;
/*	private static final int CONNECT_OUT_TIME = 5000;
	private Handler handler = new Handler() {
		 
		         @Override
		         public void handleMessage(Message msg) {
		             super.handleMessage(msg);
		             switch (msg.what) {
		             case SHOW_RESPONSE:
		                //String response = (String) msg.obj;
		                 //textView_response.setText(response);
		                 break;
		 
		             default:
		                 break;
		             }            
		         }
		 
		     };
*/

	public void sendRequestWithHttpClient(final String url,final String agr1,final HttpFinishInterface listener) {
		 Log.d("HTTPUTILS", agr1);
		
		          new Thread(new Runnable() {
		              
		              @Override
		              public void run() {
		                 //用HttpClient发送请求，分为五步
		                 //第一步：创建HttpClient对象
		                  HttpClient httpCient = new DefaultHttpClient();
		                  //第二步：创建代表请求的对象,参数是访问的服务器地址
		                  HttpPost httpPost = new HttpPost(url);
						try {
							StringEntity entiy = new StringEntity(agr1);
							  httpPost.setEntity(entiy);
						} catch (UnsupportedEncodingException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
		                
		                  
		                  try {
		                      //第三步：执行请求，获取服务器发还的相应对象
		                      HttpResponse httpResponse = httpCient.execute(httpPost);
		                      //第四步：检查相应的状态是否正常：检查状态码的值是200表示正常
		                     if (httpResponse.getStatusLine().getStatusCode() == 200) {
		                          //第五步：从相应对象当中取出数据，放到entity当中
		                          HttpEntity entity = httpResponse.getEntity();
		                          String response = EntityUtils.toString(entity,"utf-8");//将entity当中的数据转换为字符串
		                          Log.d("HTTPUTILS", response);
		                          if(null != listener)
		                          {
		                        	  listener.onRequestSuccess(response);
		                          }
		                          //在子线程中将Message对象发出去
		                     
		                      }else{
		                    	  if(null != listener)
		                          {
		                        	  listener.onRequestError("");
		                          }  
		                      }
		                      
		                  } catch (Exception e) {
		                      // TODO Auto-generated catch block
		                      e.printStackTrace();
		                  }
		                  
		              }
		          }).start();
		      } 


}
